#include"SList.h"

int test01()
{
	SLTNode* node1 = (SLTNode*)malloc(sizeof(SLTNode));
	SLTNode* node2 = (SLTNode*)malloc(sizeof(SLTNode));
	SLTNode* node3 = (SLTNode*)malloc(sizeof(SLTNode));
	SLTNode* node4 = (SLTNode*)malloc(sizeof(SLTNode));
	
	if (node1 == NULL || node2 == NULL || node3 == NULL || node4 == NULL) {
		printf("内存分配失败\n");
		// 释放已分配的内存
		if (node1) free(node1);
		if (node2) free(node2);
		if (node3) free(node3);
		if (node4) free(node4);
		return -1;
	}

	node1->data = 1;
	node2->data = 2;
	node3->data = 3;
	node4->data = 4;
	
	node1->next = node2;
	node2->next = node3;
	node3->next = node4;
	node4->next = NULL;

	SLTNode* phead = node1;

	SLTPrint(phead);
	SLTPushBack(&phead, 5);
	SLTPrint(phead);


	free(node1);
	node1 = NULL;
	free(node2);
	node2 = NULL;
	free(node3);
	node3 = NULL;
	free(node4);
	node4 = NULL;

	return 1;
}

void test02()
{
	SLTNode* plist = NULL;
	SLTPushBack(&plist, 1);
	SLTPushBack(&plist, 2);
	SLTPushBack(&plist, 3);
	SLTPushBack(&plist, 4);
	SLTPushBack(&plist, 5);
	SLTPrint(plist);
}

void test03()
{
	SLTNode* plist = NULL;
	SLTPushFront(&plist, 1);
	SLTPushFront(&plist, 2);
	SLTPushFront(&plist, 3);
	SLTPushFront(&plist, 4);
	SLTPushFront(&plist, 5);
	SLTPrint(plist);
}
void test04()
{
	SLTNode* plist = NULL;
	SLTPushFront(&plist, 1);
	//SLTPushFront(&plist, 2);
	//SLTPushFront(&plist, 3);
	//SLTPushFront(&plist, 4);
	//SLTPushFront(&plist, 5);
	SLTPopBack(&plist);
	SLTPrint(plist);
}
void test05()
{
	SLTNode* plist = NULL;
	SLTPushFront(&plist, 1);
	SLTPushFront(&plist, 2);
	SLTPushFront(&plist, 3);
	SLTPushFront(&plist, 4);
	SLTPushFront(&plist, 5);
	SLTPopX(&plist, 2);
	SLTPopX(&plist, 2);
	SLTPopX(&plist, 1);
	SLTPopX(&plist, 7);
	SLTPopX(&plist, 0);
	SLTPrint(plist);
}
void test06()
{
	SLTNode* plist = NULL;
	SLTPushFront(&plist, 1);
	SLTPushFront(&plist, 2);
	SLTPushFront(&plist, 3);
	SLTPushFront(&plist, 4);
	SLTPushFront(&plist, 5);
	SLTNode* pos = SLTFind(plist, 3);
	if (pos)
	{
		printf("找到了\n");
	}
	else
	{
		printf("未找到\n");
	}
	SLTPrint(plist);
}

void test07()
{
	SLTNode* node1 = (SLTNode*)malloc(sizeof(SLTNode));
	SLTNode* node2 = (SLTNode*)malloc(sizeof(SLTNode));
	SLTNode* node3 = (SLTNode*)malloc(sizeof(SLTNode));
	SLTNode* node4 = (SLTNode*)malloc(sizeof(SLTNode));

	if (node1 == NULL || node2 == NULL || node3 == NULL || node4 == NULL) {
		printf("内存分配失败\n");
		// 释放已分配的内存
		if (node1) free(node1);
		if (node2) free(node2);
		if (node3) free(node3);
		if (node4) free(node4);
		return;
	}

	node1->data = 1;
	node2->data = 2;
	node3->data = 3;
	node4->data = 4;

	node1->next = node2;
	node2->next = node3;
	node3->next = node4;
	node4->next = NULL;

	SLTNode* phead = node1;

	SLTInsert(&phead, node2, 7);
	SLTInsertAfter(node1, 8);
	SLTPrint(phead);
}


void test08()
{
	SLTNode* node1 = (SLTNode*)malloc(sizeof(SLTNode));
	SLTNode* node2 = (SLTNode*)malloc(sizeof(SLTNode));
	SLTNode* node3 = (SLTNode*)malloc(sizeof(SLTNode));
	SLTNode* node4 = (SLTNode*)malloc(sizeof(SLTNode));

	if (node1 == NULL || node2 == NULL || node3 == NULL || node4 == NULL) {
		printf("内存分配失败\n");
		// 释放已分配的内存
		if (node1) free(node1);
		if (node2) free(node2);
		if (node3) free(node3);
		if (node4) free(node4);
		return;
	}

	node1->data = 1;
	node2->data = 2;
	node3->data = 3;
	node4->data = 4;

	node1->next = node2;
	node2->next = node3;
	node3->next = node4;
	node4->next = NULL;

	SLTNode* phead = node1;

	//SLTErase(&phead, node2);
	SLTEraseAfter(node1);
	SLTPrint(phead);
}


int main()
{
	//test01();
	//test02();
	//test03();
	//test04();
	//test05();
	//test06();
	//test07();
	test08();
	return 0;
}
